package com.itheima.reggie.controller;

import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.User;
import com.itheima.reggie.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

// app前台用户
@RestController
public class UserController {

    @Autowired
    public UserService userService;

    //用户登录验证
    @PostMapping("/user/sendMsg")
    public ResultInfo sendMsg(@RequestBody Map<String, String> param) {
        //通过key获取value请求参数
        String phone = param.get("phone");
        //调用service发短信
        userService.sendMsg(phone);
        //返回结果
        return ResultInfo.success();
    }


    // 登录注册
    @PostMapping("user/login")
    public ResultInfo login(@RequestBody Map<String, String> param) {
        //通过key获取value请求参数
        String phone = param.get("phone");
        String code = param.get("code");
        //调用service登录 or 注册  (这里的返回是service层需要返回制造的令牌)
        ResultInfo resultInfo = userService.login(phone, code);
        //返回结果
        return resultInfo;
    }

    //用户退出
    @PostMapping("/user/logout")
    public ResultInfo logout(){
        //调用
        userService.logout();
        //返回
        return ResultInfo.success();
    }

}
